<!DOCTYPE html>
<html lang="zh_CN">
<head>
    <#-- 包含其他文件的内容 -->
    <#include "../common/header.ftl"/>
    <script src="/js/ckeditor/ckeditor.js"></script>

        <script>
            $(function () {
                //添加/编辑按钮
                $(".inputBtn").click(function () {
                    $("#inputModal input").val('');
                    var json = $(this).data('json');
                    if(json){
                        $("#inputModal input[name=id]").val(json.id);
                        $("#inputModal input[name=title]").val(json.title);
                        $("#inputModal input[name=sequence]").val(json.sequence);
                        // 封面
                        $("#coverImg").attr('src',json.coverUrl);
                        $("#inputModal input[name=coverUrl]").val(json.coverUrl);
                        $("#inputModal select[name=state]").val(json.state); // 变成字符串在回显
                        // 回显大攻略id
                        $("#strategySelect").val(json.strategyId);
                        // 根据大攻略id查询攻略目录数据
                        $.get('/strategyCatalog/listByStrategyId.do',{strategyId:json.strategyId},function (data) {
                            var temp = "";
                            $.each(data,function (index, ele) {
                                temp += '<option value="'+ele.id+'">'+ele.name+'</option>';
                            })
                            $("#catalogSelect").html(temp);
                            // 回显攻略目录下拉框
                            $("#catalogSelect").val(json.catalogId);
                        })
                        // 获取攻略的内容
                        $.get('/strategyDetail/getContentById.do',{id:json.id},function (data) {
                            ckeditor.setData(data.content);
                        })
                    }else {
                        ckeditor.setData('');
                    }
                    $("#inputModal").modal('show');
                })

                //提交按钮
                $(".submitBtn").click(function () {
                    //获取富文本插件中的内容
                    var content = ckeditor.getData();
                    //设置到表单中的控件
                    $("#editor1").html(content);

                    $("#editForm").ajaxSubmit(function (data) {
                        handleMessage(data);
                    })
                })

                //大攻略下拉框值改变事件
                $("#strategySelect").change(function () {
                    //获取大攻略的值
                    var strategyId = $(this).val();
                    //获取该攻略下的所有目录
                    $.get('/strategyCatalog/listByStrategyId.do',{strategyId:strategyId},function (data) {
                        var temp = "";
                        $.each(data,function (index, ele) {
                            temp += '<option value="'+ele.id+'">'+ele.name+'</option>';
                        })
                        $("#catalogSelect").html(temp);
                    })

                })

                //富文本插件对象
                var ckeditor = CKEDITOR.replace( 'editor1' );

            })
        </script>
</head>
<body>

<div class="container " style="margin-top: 20px">
    <#include "../common/top.ftl"/>
    <div class="row">
        <div class="col-sm-3">
            <#-- 往数据模型中填充一个属性 -->
            <#assign currentMenu="strategyDetail"/>
            <#include "../common/menu.ftl"/>
        </div>
        <div class="col-sm-9">
            <div class="row">
                <div class="col-sm-12">
                    <h1 class="page-head-line">攻略文章管理</h1>
                </div>
            </div>
            <!--高级查询--->
            <form class="form-inline" id="searchForm" action="/strategyDetail/list.do" method="post">
                <input type="hidden" name="currentPage" id="currentPage" value="1">
                <div class="form-group">
                    <label>关键字:</label>
                    <input type="text" class="form-control" name="keyword"
                           value="${(qo.keyword)!}" placeholder="请输入标题">
                </div>
                <button id="btn_query" class="btn btn-info">
                    <span class="glyphicon glyphicon-search"></span> 查询
                </button>
                <a role="button" class="btn btn-success inputBtn">
                    <span class="glyphicon glyphicon-plus"></span> 添加
                </a>
            </form>

            <table class="table table-striped table-hover" >
                <tr>
                    <th>编号</th>
                    <th>标题</th>
                    <th>封面</th>
                    <th>发布时间</th>
                    <th>排序</th>
                    <th>攻略类别</th>
                    <th>状态</th>
                    <th>操作</th>
                </tr>

            <#-- 循环迭代指令 -->
                <#list pageInfo.list as entity>
                    <tr>
                        <td>${entity_index+1}</td>
                        <td>${entity.title}</td>
                        <td><img src="${entity.coverUrl}" width="50px"/></td>
                        <td>${(entity.releaseTime?string("yyyy-MM-dd"))!}</td>
                        <td>${entity.sequence}</td>
                        <td>${entity.catalog.name}</td>
                        <td>${entity.stateName}</td>
                        <td>
                            <a class="btn btn-warning btn-sm inputBtn"  data-json='${entity.json}'  >
                                <span class="glyphicon glyphicon-pencil"></span> 修改
                            </a>
                        </td>
                    </tr>
                </#list>
            </table>
            <#include "../common/page.ftl"/>
        </div>
    </div>
</div>


<!-- 模态框必须作为body的子标签 -->
<div class="modal fade" id="inputModal" tabindex="-1" role="dialog"
     aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title" id="myModalLabel">新增/编辑</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" action="/strategyDetail/saveOrUpdate.do" method="post" id="editForm">

                    <input type="hidden" name="id">
                    <div class="form-group" >
                        <label class="col-sm-4 control-label">标题：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" name="title" >
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">封面：</label>
                        <div class="col-sm-6">
                            <img id="coverImg" width="100%"/>
                            <input type="hidden"  name="coverUrl" >
                            <input type="file" class="form-control" name="file" >
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">所属攻略：</label>
                        <div class="col-sm-6">
                            <select class="form-control" id="strategySelect" >
                                <#list strategies as s>
                                    <option value="${s.id}">${s.title}</option>
                                </#list>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">攻略类别：</label>
                        <div class="col-sm-6">
                            <select class="form-control" id="catalogSelect"  name="catalog.id" >

                            </select>
                        </div>
                    </div>
                    <div class="form-group" >
                        <label class="col-sm-4 control-label">序号：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" name="sequence" >
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">状态：</label>
                        <div class="col-sm-6">
                            <select class="form-control" name="state" >
                                <option value="0">草稿</option>
                                <option value="1">发布</option>
                                <option value="-1">禁用</option>
                            </select>
                        </div>
                    </div>

                    <textarea name="strategyContent.content" id="editor1" rows="10" cols="80">
                    </textarea>

                </form>


            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-danger" data-dismiss="modal">
                    <span class="glyphicon glyphicon-off"></span> 戳我关闭
                </button>
                <button type="button" class="btn btn-primary submitBtn">
                    <span class="glyphicon glyphicon-saved"></span> 朕要保存
                </button>
            </div>
        </div>
    </div>
</div>





</body>
</html>